package com.zhang.reggie.config;

import org.springframework.core.task.TaskDecorator;

/**
 * 实现明天理想的唯一障碍是今天的疑虑
 *
 * @Author {张旭军}
 * @Date: 2023/02/04/ 23:03
 * @description
 */
public class ContextTaskDecorator implements TaskDecorator {
    @Override
    public Runnable decorate(Runnable runnable) {

        //父线程
        String s = OauthContext.get();

        return ()->{
            try {
                //设置子线程的值
                OauthContext.set(s);
                runnable.run();
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                // 线程结束，清空这些信息，否则可能造成内存泄漏
                OauthContext.clear();
            }
        };
    }
}
